package org.jacp.demo.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.border.MatteBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.jacp.demo.entities.Employee;
import org.jacp.demo.services.EmployeeService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.explodingpixels.macwidgets.HudWidgetFactory;
import com.explodingpixels.macwidgets.MacWidgetFactory;

public class EmployeeUI extends JFrame {
	/**
     * 
     */
	private static final long serialVersionUID = -8260290677491890210L;
	private JTable table;
	static List<Employee> employees;
	private JTextField firstname;
	private JTextField mindint;
	private JTextField lastname;
	private JLabel lblFirstname;
	private JLabel lblMidint;
	private JLabel lblLasname;
	private JTextField phoneno;
	private JLabel lblPhoneno;
	private JTextField job;
	private JLabel lblJob;
	private JTextField edevel;
	private JLabel lblEdevel;
	private JTextField sex;
	private JLabel lblSex;
	private JTextField salery;
	private JLabel lblSalary;
	private JTextField bonus;
	private JLabel lblBonus;
	private JTextField commission;
	private JLabel lblCommission;
	private JButton btnClear;
	private JButton btnSave;
	private JLabel label;
	private JLabel label_1;
	private EmployeeTableModel tableModel;
	private JPanel editorPanel;
	private static final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
			new String[] { "org/jacp/demo/resource/services.xml" });
	private static EmployeeService employeeService;
	private Employee workingCopy;
	private JPanel panel;
	private JSplitPane middleSplitPane;
	private JLabel lblName;
	private JTextField searchNameField;
	private JSplitPane mainPanel;
	private JPanel panel_3;
	private JPanel searchPanel;
	private JPanel panel_4;
	private JPanel panel_6;
	private JLabel lblFirstname_1;
	private JTextField searchFirstNameTxt;
	private JLabel lbl_job;
	private JLabel lbl_Salery;
	private JLabel lbl_sex;
	private JTextField txt_Job;
	private JTextField txt_salery;
	private JTextField txt_sex;
	private JPanel panel_7;
	private JPanel image_panel;

	public EmployeeUI() {
		this.setSize(1024, 786);
		mainPanel = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
		mainPanel.setSize(1024, 600);
		mainPanel.setDividerLocation(0.18);
		mainPanel.setOneTouchExpandable(true);
		getContentPane().add(mainPanel);

		panel = new JPanel();
		initSearchBar();
		mainPanel.add(panel);

		searchPanel = new JPanel();
		panel.add(searchPanel, BorderLayout.NORTH);
		searchPanel.setLayout(new BorderLayout(0, 0));

		panel_7 = new JPanel();
		searchPanel.add(panel_7);
		GridBagLayout gbl_panel_7 = new GridBagLayout();
		gbl_panel_7.columnWidths = new int[] { 74, 150, 150, 0, 300, 0, 0 };
		gbl_panel_7.rowHeights = new int[] { 28, 0, 0, 0, 0, 0 };
		gbl_panel_7.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0,
				0.0, Double.MIN_VALUE };
		gbl_panel_7.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0,
				Double.MIN_VALUE };
		panel_7.setLayout(gbl_panel_7);

		lblName = HudWidgetFactory.createHudLabel("lastname");
		GridBagConstraints gbc_lblName = new GridBagConstraints();
		gbc_lblName.anchor = GridBagConstraints.EAST;
		gbc_lblName.insets = new Insets(0, 0, 5, 5);
		gbc_lblName.gridx = 0;
		gbc_lblName.gridy = 0;
		panel_7.add(lblName, gbc_lblName);
		lblName.setHorizontalAlignment(SwingConstants.LEFT);

		searchNameField = HudWidgetFactory.createHudTextField("");
		GridBagConstraints gbc_searchNameField = new GridBagConstraints();
		gbc_searchNameField.gridwidth = 2;
		gbc_searchNameField.fill = GridBagConstraints.HORIZONTAL;
		gbc_searchNameField.insets = new Insets(0, 0, 5, 5);
		gbc_searchNameField.anchor = GridBagConstraints.NORTH;
		gbc_searchNameField.gridx = 1;
		gbc_searchNameField.gridy = 0;
		panel_7.add(searchNameField, gbc_searchNameField);
		searchNameField.setColumns(1);

		lbl_Salery = HudWidgetFactory.createHudLabel("Salery");
		GridBagConstraints gbc_lbl_Salery = new GridBagConstraints();
		gbc_lbl_Salery.anchor = GridBagConstraints.EAST;
		gbc_lbl_Salery.insets = new Insets(0, 0, 5, 5);
		gbc_lbl_Salery.gridx = 3;
		gbc_lbl_Salery.gridy = 0;
		panel_7.add(lbl_Salery, gbc_lbl_Salery);

		txt_salery = HudWidgetFactory.createHudTextField("");
		GridBagConstraints gbc_txt_salery = new GridBagConstraints();
		gbc_txt_salery.fill = GridBagConstraints.HORIZONTAL;
		gbc_txt_salery.insets = new Insets(0, 0, 5, 5);
		gbc_txt_salery.gridx = 4;
		gbc_txt_salery.gridy = 0;
		panel_7.add(txt_salery, gbc_txt_salery);
		txt_salery.setColumns(10);

		lblFirstname_1 = HudWidgetFactory.createHudLabel("firstname");
		GridBagConstraints gbc_lblFirstname_1 = new GridBagConstraints();
		gbc_lblFirstname_1.anchor = GridBagConstraints.EAST;
		gbc_lblFirstname_1.insets = new Insets(0, 0, 5, 5);
		gbc_lblFirstname_1.gridx = 0;
		gbc_lblFirstname_1.gridy = 1;
		panel_7.add(lblFirstname_1, gbc_lblFirstname_1);

		searchFirstNameTxt = HudWidgetFactory.createHudTextField("");
		GridBagConstraints gbc_searchFirstNameTxt = new GridBagConstraints();
		gbc_searchFirstNameTxt.fill = GridBagConstraints.HORIZONTAL;
		gbc_searchFirstNameTxt.gridwidth = 2;
		gbc_searchFirstNameTxt.insets = new Insets(0, 0, 5, 5);
		gbc_searchFirstNameTxt.gridx = 1;
		gbc_searchFirstNameTxt.gridy = 1;
		panel_7.add(searchFirstNameTxt, gbc_searchFirstNameTxt);
		searchFirstNameTxt.setColumns(10);

		lbl_sex = HudWidgetFactory.createHudLabel("Sex");
		GridBagConstraints gbc_lbl_sex = new GridBagConstraints();
		gbc_lbl_sex.anchor = GridBagConstraints.EAST;
		gbc_lbl_sex.insets = new Insets(0, 0, 5, 5);
		gbc_lbl_sex.gridx = 3;
		gbc_lbl_sex.gridy = 1;
		panel_7.add(lbl_sex, gbc_lbl_sex);

		txt_sex = HudWidgetFactory.createHudTextField("");
		GridBagConstraints gbc_txt_sex = new GridBagConstraints();
		gbc_txt_sex.insets = new Insets(0, 0, 5, 5);
		gbc_txt_sex.fill = GridBagConstraints.HORIZONTAL;
		gbc_txt_sex.gridx = 4;
		gbc_txt_sex.gridy = 1;
		panel_7.add(txt_sex, gbc_txt_sex);
		txt_sex.setColumns(10);

		lbl_job = HudWidgetFactory.createHudLabel("Job");
		GridBagConstraints gbc_lbl_job = new GridBagConstraints();
		gbc_lbl_job.anchor = GridBagConstraints.EAST;
		gbc_lbl_job.insets = new Insets(0, 0, 5, 5);
		gbc_lbl_job.gridx = 0;
		gbc_lbl_job.gridy = 2;
		panel_7.add(lbl_job, gbc_lbl_job);

		txt_Job = HudWidgetFactory.createHudTextField("");
		GridBagConstraints gbc_txt_Job = new GridBagConstraints();
		gbc_txt_Job.fill = GridBagConstraints.HORIZONTAL;
		gbc_txt_Job.gridwidth = 2;
		gbc_txt_Job.insets = new Insets(0, 0, 5, 5);
		gbc_txt_Job.gridx = 1;
		gbc_txt_Job.gridy = 2;
		panel_7.add(txt_Job, gbc_txt_Job);
		txt_Job.setColumns(10);
		final JButton button = HudWidgetFactory.createHudButton("search");
		GridBagConstraints gbc_button = new GridBagConstraints();
		gbc_button.anchor = GridBagConstraints.EAST;
		gbc_button.insets = new Insets(0, 0, 5, 5);
		gbc_button.gridx = 4;
		gbc_button.gridy = 2;
		panel_7.add(button, gbc_button);
		button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(final ActionEvent arg0) {
				search();
			}
		});

		middleSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
		middleSplitPane.setResizeWeight(0.7);
		middleSplitPane.setBorder(new MatteBorder(1, 1, 1, 1,
				(Color) new Color(0, 0, 0)));
		middleSplitPane.setDividerLocation(850);
		middleSplitPane.setOneTouchExpandable(true);
		middleSplitPane.setContinuousLayout(true);
		middleSplitPane.setSize(1024, 650);
		mainPanel.add(middleSplitPane);

		initTable();
		final JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setSize(850, 600);
		middleSplitPane.setLeftComponent(scrollPane);

		editorPanel = new JPanel();
		scrollPane.setSize(174, 600);
		editorPanel.setSize(100, 500);
		middleSplitPane.setRightComponent(editorPanel);
		initEditorPane();
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);

	}

	protected void initSearchBar() {
		panel.setLayout(new BorderLayout(0, 0));
	}

	protected void search() {
		String value = searchNameField.getText();
		String lastName = searchFirstNameTxt.getText();
		tableModel.flush();
		if (isBlank(value) && isBlank(lastName)) {
			tableModel.addEntries(employeeService.findAll());
		} else {

			tableModel.addEntries(employeeService.findByEmployeeName(value));
		}
	}

	private boolean isBlank(String value) {
		return (value == null || value.equals(""));
	}

	public void initEditorPane() {

		/*
		 * label = HudWidgetFactory.createHudLabel (""); editorPanel.add(label);
		 * 
		 * label_1 = HudWidgetFactory.createHudLabel ("");
		 * editorPanel.add(label_1);
		 */

		JPanel panel_2 = new JPanel();

		panel_3 = new JPanel();

		btnClear = HudWidgetFactory.createHudButton("clear");
		panel_3.add(btnClear);

		btnSave = HudWidgetFactory.createHudButton("save");
		panel_3.add(btnSave);
		btnSave.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(final ActionEvent arg0) {
				saveEmployee();
			}
		});
		btnClear.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(final ActionEvent arg0) {
				workingCopy = null;
				clearValue();
			}
		});
		editorPanel.setLayout(new BorderLayout(0, 0));
		GridBagLayout gbl_panel_2 = new GridBagLayout();
		gbl_panel_2.columnWidths = new int[] { 80, 80, 0 };
		gbl_panel_2.rowHeights = new int[] { 0, 28, 28, 28, 28, 28, 28, 28, 28,
				28, 28, 0 };
		gbl_panel_2.columnWeights = new double[] { 0.0, 0.0, Double.MIN_VALUE };
		gbl_panel_2.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
				0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
		panel_2.setLayout(gbl_panel_2);

		lblCommission = HudWidgetFactory.createHudLabel("COMMISSION");
		GridBagConstraints gbc_lblCommission = new GridBagConstraints();
		gbc_lblCommission.fill = GridBagConstraints.BOTH;
		gbc_lblCommission.insets = new Insets(0, 0, 5, 5);
		gbc_lblCommission.gridx = 0;
		gbc_lblCommission.gridy = 0;
		panel_2.add(lblCommission, gbc_lblCommission);

		commission = HudWidgetFactory.createHudTextField("");
		GridBagConstraints gbc_commission = new GridBagConstraints();
		gbc_commission.insets = new Insets(0, 0, 5, 0);
		gbc_commission.fill = GridBagConstraints.BOTH;
		gbc_commission.gridx = 1;
		gbc_commission.gridy = 0;
		panel_2.add(commission, gbc_commission);
		commission.setColumns(10);

		lblFirstname = HudWidgetFactory.createHudLabel("FIRSTNAME");
		GridBagConstraints gbc_lblFirstname = new GridBagConstraints();
		gbc_lblFirstname.fill = GridBagConstraints.BOTH;
		gbc_lblFirstname.insets = new Insets(0, 0, 5, 5);
		gbc_lblFirstname.gridx = 0;
		gbc_lblFirstname.gridy = 1;
		panel_2.add(lblFirstname, gbc_lblFirstname);

		firstname = HudWidgetFactory.createHudTextField("");
		GridBagConstraints gbc_firstname = new GridBagConstraints();
		gbc_firstname.fill = GridBagConstraints.BOTH;
		gbc_firstname.insets = new Insets(0, 0, 5, 0);
		gbc_firstname.gridx = 1;
		gbc_firstname.gridy = 1;
		panel_2.add(firstname, gbc_firstname);

		firstname.setColumns(10);

		lblMidint = HudWidgetFactory.createHudLabel("MIDINT");
		GridBagConstraints gbc_lblMidint = new GridBagConstraints();
		gbc_lblMidint.fill = GridBagConstraints.BOTH;
		gbc_lblMidint.insets = new Insets(0, 0, 5, 5);
		gbc_lblMidint.gridx = 0;
		gbc_lblMidint.gridy = 2;
		panel_2.add(lblMidint, gbc_lblMidint);

		mindint = HudWidgetFactory.createHudTextField("");
		GridBagConstraints gbc_mindint = new GridBagConstraints();
		gbc_mindint.fill = GridBagConstraints.BOTH;
		gbc_mindint.insets = new Insets(0, 0, 5, 0);
		gbc_mindint.gridx = 1;
		gbc_mindint.gridy = 2;
		panel_2.add(mindint, gbc_mindint);

		lblLasname = HudWidgetFactory.createHudLabel("LASTNAME");
		GridBagConstraints gbc_lblLasname = new GridBagConstraints();
		gbc_lblLasname.fill = GridBagConstraints.BOTH;
		gbc_lblLasname.insets = new Insets(0, 0, 5, 5);
		gbc_lblLasname.gridx = 0;
		gbc_lblLasname.gridy = 3;
		panel_2.add(lblLasname, gbc_lblLasname);

		lastname = HudWidgetFactory.createHudTextField("");
		GridBagConstraints gbc_lastname = new GridBagConstraints();
		gbc_lastname.fill = GridBagConstraints.BOTH;
		gbc_lastname.insets = new Insets(0, 0, 5, 0);
		gbc_lastname.gridx = 1;
		gbc_lastname.gridy = 3;
		panel_2.add(lastname, gbc_lastname);

		lblPhoneno = HudWidgetFactory.createHudLabel("PHONENR");
		GridBagConstraints gbc_lblPhoneno = new GridBagConstraints();
		gbc_lblPhoneno.fill = GridBagConstraints.BOTH;
		gbc_lblPhoneno.insets = new Insets(0, 0, 5, 5);
		gbc_lblPhoneno.gridx = 0;
		gbc_lblPhoneno.gridy = 4;
		panel_2.add(lblPhoneno, gbc_lblPhoneno);

		phoneno = HudWidgetFactory.createHudTextField("");
		GridBagConstraints gbc_phoneno = new GridBagConstraints();
		gbc_phoneno.fill = GridBagConstraints.BOTH;
		gbc_phoneno.insets = new Insets(0, 0, 5, 0);
		gbc_phoneno.gridx = 1;
		gbc_phoneno.gridy = 4;
		panel_2.add(phoneno, gbc_phoneno);
		phoneno.setColumns(10);

		lblJob = HudWidgetFactory.createHudLabel("JOB");
		GridBagConstraints gbc_lblJob = new GridBagConstraints();
		gbc_lblJob.fill = GridBagConstraints.BOTH;
		gbc_lblJob.insets = new Insets(0, 0, 5, 5);
		gbc_lblJob.gridx = 0;
		gbc_lblJob.gridy = 5;
		panel_2.add(lblJob, gbc_lblJob);

		job = HudWidgetFactory.createHudTextField("");
		GridBagConstraints gbc_job = new GridBagConstraints();
		gbc_job.fill = GridBagConstraints.BOTH;
		gbc_job.insets = new Insets(0, 0, 5, 0);
		gbc_job.gridx = 1;
		gbc_job.gridy = 5;
		panel_2.add(job, gbc_job);
		job.setColumns(10);

		lblEdevel = HudWidgetFactory.createHudLabel("EDEVEL");
		GridBagConstraints gbc_lblEdevel = new GridBagConstraints();
		gbc_lblEdevel.fill = GridBagConstraints.BOTH;
		gbc_lblEdevel.insets = new Insets(0, 0, 5, 5);
		gbc_lblEdevel.gridx = 0;
		gbc_lblEdevel.gridy = 6;
		panel_2.add(lblEdevel, gbc_lblEdevel);

		edevel = HudWidgetFactory.createHudTextField("");
		GridBagConstraints gbc_edevel = new GridBagConstraints();
		gbc_edevel.fill = GridBagConstraints.BOTH;
		gbc_edevel.insets = new Insets(0, 0, 5, 0);
		gbc_edevel.gridx = 1;
		gbc_edevel.gridy = 6;
		panel_2.add(edevel, gbc_edevel);
		edevel.setColumns(10);

		lblSex = HudWidgetFactory.createHudLabel("SEX");
		GridBagConstraints gbc_lblSex = new GridBagConstraints();
		gbc_lblSex.fill = GridBagConstraints.BOTH;
		gbc_lblSex.insets = new Insets(0, 0, 5, 5);
		gbc_lblSex.gridx = 0;
		gbc_lblSex.gridy = 7;
		panel_2.add(lblSex, gbc_lblSex);

		sex = HudWidgetFactory.createHudTextField("");
		GridBagConstraints gbc_sex = new GridBagConstraints();
		gbc_sex.fill = GridBagConstraints.BOTH;
		gbc_sex.insets = new Insets(0, 0, 5, 0);
		gbc_sex.gridx = 1;
		gbc_sex.gridy = 7;
		panel_2.add(sex, gbc_sex);
		sex.setColumns(10);

		lblSalary = HudWidgetFactory.createHudLabel("SALARY");
		GridBagConstraints gbc_lblSalary = new GridBagConstraints();
		gbc_lblSalary.fill = GridBagConstraints.BOTH;
		gbc_lblSalary.insets = new Insets(0, 0, 5, 5);
		gbc_lblSalary.gridx = 0;
		gbc_lblSalary.gridy = 8;
		panel_2.add(lblSalary, gbc_lblSalary);

		salery = HudWidgetFactory.createHudTextField("");
		GridBagConstraints gbc_salery = new GridBagConstraints();
		gbc_salery.fill = GridBagConstraints.BOTH;
		gbc_salery.insets = new Insets(0, 0, 5, 0);
		gbc_salery.gridx = 1;
		gbc_salery.gridy = 8;
		panel_2.add(salery, gbc_salery);
		salery.setColumns(10);

		lblBonus = HudWidgetFactory.createHudLabel("BONUS");
		GridBagConstraints gbc_lblBonus = new GridBagConstraints();
		gbc_lblBonus.fill = GridBagConstraints.BOTH;
		gbc_lblBonus.insets = new Insets(0, 0, 5, 5);
		gbc_lblBonus.gridx = 0;
		gbc_lblBonus.gridy = 9;
		panel_2.add(lblBonus, gbc_lblBonus);

		bonus = HudWidgetFactory.createHudTextField("");
		GridBagConstraints gbc_bonus = new GridBagConstraints();
		gbc_bonus.fill = GridBagConstraints.BOTH;
		gbc_bonus.insets = new Insets(0, 0, 5, 0);
		gbc_bonus.gridx = 1;
		gbc_bonus.gridy = 9;
		panel_2.add(bonus, gbc_bonus);
		bonus.setColumns(10);
		editorPanel.add(panel_2, BorderLayout.CENTER);
		editorPanel.add(panel_3, BorderLayout.SOUTH);

		panel_4 = new JPanel();
		editorPanel.add(panel_4, BorderLayout.EAST);

		panel_6 = new JPanel();
		editorPanel.add(panel_6, BorderLayout.WEST);

		image_panel = new JPanel();
		editorPanel.add(image_panel, BorderLayout.NORTH);
		image_panel.setLayout(new BorderLayout(0, 0));
	}

	public void initTable() {

		final String[] columnNames = { "ID", "FIRSTNME", "MIDINIT", "LASTNAME",
				"PHONENO", "JOB", "EDLEVEL", "SEX", "SALERY", "BONUS",
				"COMMISSION" };
		tableModel = new EmployeeTableModel(columnNames,
				(List<Employee>) (employees != null ? employees
						: Collections.emptyList()));
		// table = new JTable();
		table = MacWidgetFactory.createITunesTable(tableModel);
		// table.setAutoCreateRowSorter(true);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		final ListSelectionModel listSelectionModel = table.getSelectionModel();
		listSelectionModel
				.addListSelectionListener(new ListSelectionListener() {
					@Override
					public void valueChanged(final ListSelectionEvent e) {
						if (!e.getValueIsAdjusting()) {
							final int selectedRow = table.getSelectedRow();
							if (selectedRow >= 0) {
								final Employee emp = tableModel
										.getRecordAt(selectedRow);
								setValueToTextfield(emp);
							}
						}
					}

				});
		table.setModel(tableModel);
		table.setSurrendersFocusOnKeystroke(true);
		if (!tableModel.hasEmptyRow()) {
			tableModel.addEmptyRow();
		}

	}

	protected void saveEmployee() {
		final boolean copy = showCopyDialog();
		if (copy) {
			final Employee myEmp = new Employee(firstname.getText(),
					mindint.getText(), lastname.getText(), phoneno.getText(),
					job.getText(), Integer.valueOf(edevel.getText()), sex
							.getText().charAt(0), Double.valueOf(salery
							.getText()), Double.valueOf(bonus.getText()),
					Double.valueOf(commission.getText()), null, null, null);
			employeeService.save(myEmp);

		} else {
			if (workingCopy != null) {
				employeeService.update(setValueToEmployee(workingCopy));
				tableModel.fireTableDataChanged();
			}

		}
	}

	protected boolean showCopyDialog() {
		final JOptionPane pane = new JOptionPane("Create a copy?");
		final Object[] options = new String[] { "Yes", "No" };
		pane.setOptions(options);
		final JDialog dialog = pane.createDialog(new JFrame(), "Save");
		dialog.show();
		final Object obj = pane.getValue();
		return obj.equals("Yes") ? true : false;
	}

	protected void save() {

	}

	protected void clearValue() {
		getImageBySex("");
		firstname.setText(null);
		lastname.setText(null);
		mindint.setText(null);
		phoneno.setText(null);
		job.setText(null);
		edevel.setText(null);
		sex.setText(null);
		salery.setText(null);
		bonus.setText(null);
		commission.setText(null);
	}

	private BufferedImage getImageBySex(String sex) {
		sex = sex == null ? "" : sex;
		BufferedImage image = null;
		try {
			String file = "".equals(sex) ? "empty.jpg"
					: "F".equals(sex) ? "woman.jpg" : "man.jpg";
			image = ImageIO.read(new File("../data/" + file));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return image;

	}

	public Employee setValueToEmployee(final Employee emp) {
		emp.setFirstName(firstname.getText());
		emp.setLastName(lastname.getText());
		emp.setMidInitial(mindint.getText());
		emp.setPhoneNumber(phoneno.getText());
		emp.setJob(job.getText());
		emp.setEducationLevel(Integer.valueOf(edevel.getText()));
		emp.setSex(sex.getText().charAt(0));
		emp.setSalary(Double.valueOf(salery.getText()));
		emp.setBonus(Double.valueOf(bonus.getText()));
		emp.setCommission(Double.valueOf(commission.getText()));
		clearValue();
		workingCopy = null;
		return emp;
	}

	public void setValueToTextfield(final Employee emp) {
		firstname.setText(emp.getFirstName());
		lastname.setText(emp.getLastName());
		mindint.setText(emp.getMidInitial());
		phoneno.setText(emp.getPhoneNumber());
		job.setText(emp.getJob());
		edevel.setText(emp.getEducationLevel() + "");
		sex.setText(emp.getSex() + "");
		salery.setText(emp.getSalary() + "");
		bonus.setText(emp.getBonus() + "");
		commission.setText(emp.getCommission() + "");
		workingCopy = emp;
	}

	public static void main(final String args[]) {
		employeeService = (EmployeeService) context.getBean("employeeService");
		employees = employeeService.findAll();
		final EmployeeUI ui = new EmployeeUI();
		ui.setVisible(true);
	}
}
